//
//  RNOLUnitTool.m
//  RongNiuOnline
//
//  Created by apple on 2018/4/4.
//  Copyright © 2018年 rongniu. All rights reserved.
//

#import "RNOLUnitTool.h"
#import <CoreText/CoreText.h>
#import <objc/runtime.h>


@implementation RNOLUnitTool

/** Label 颜色 字号*/
+ (UILabel *)createLabelWithTextColor:(UIColor *)textColor
                             fontSize:(CGFloat )size{
    UILabel *label = [[UILabel alloc]init];
    [label setTextColor:textColor];
    [label setFont:[UIFont systemFontOfSize:size]];
    return label;
}

/** Label 文字 字号*/
+ (UILabel *)createLabelWithText:(NSString *)text
                        fontSize:(CGFloat )size{
    
    UILabel *label = [[UILabel alloc]init];
    [label setText:text];
    [label setFont:[UIFont systemFontOfSize:size]];
    return label;
}

/** Label 字色 文字 字号*/
+ (UILabel *)createLabelWithText:(NSString *)text
                       textColor:(UIColor *)textColor
                        fontSize:(CGFloat )size{
    
    UILabel *label = [[UILabel alloc]init];
    [label setText:text];
    [label setTextColor:textColor];
    [label setFont:[UIFont systemFontOfSize:size]];
    return label;
}

/** Label 字色 文字 字号 对其*/
+ (UILabel *)createLabelWithText:(NSString *)text
                       textColor:(UIColor *)textColor
                        fontSize:(CGFloat )size
                   textAlignment:(NSTextAlignment)textAlignment{
    
    UILabel *label = [[UILabel alloc]init];
    [label setText:text];
    [label setTextColor:textColor];
    [label setFont:[UIFont systemFontOfSize:size]];
    [label setTextAlignment:textAlignment];
    return label;
}

+(UILabel *)createLabelWithBackgroundColor:(UIColor *)backgroundColor
                                 textColor:(UIColor *)textColor
                             textAlignment:(NSTextAlignment)textAlignment
                             numberOfLines:(NSInteger )numberOfLines
                                      text:(NSString *)text
                                  fontSize:(CGFloat)size
{
    UILabel *label = [[UILabel alloc]init];
    return label;
    
}


/** UIImageView  iamge*/
+(UIImageView *)createImageViewWithImage:(UIImage *)image{
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView setImage:image];
    return imageView;
}

/** UIView  颜色*/
+ (UIView *)createViewWithBackgroundColor:(UIColor *)backgroundColor{
    
    UIView *view = [[UIView alloc]init];
    [view setBackgroundColor:backgroundColor];
    return view;
}

#pragma mark - 设置控件圆角
+ (void )makeCorner:(CGFloat)corner
               view:(UIView *)view{
    
    CALayer *layer = view.layer;
    layer.cornerRadius = corner;
    layer.masksToBounds = YES;
    layer.shouldRasterize = YES;
    layer.rasterizationScale = [UIScreen mainScreen].scale;
}

#pragma mark - 给控件添加单击手势
+ (void)addClickEvent:(id)target
               action:(SEL)action
                owner:(UIView *)view{
    
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
    gesture.numberOfTouchesRequired = 1;
    view.userInteractionEnabled = YES;
    [view addGestureRecognizer:gesture];
    
}

#pragma mark - 给控件设置边框的宽度和颜色
+ (void)makeWidth:(CGFloat)width
             view:(UIView *)view
            color:(UIColor *)color{
    
    CALayer * fileslayer = [view layer];
    fileslayer.borderColor = [color CGColor];
    fileslayer.borderWidth = width;
    
}

#pragma mark 底部灰色线条
+(void)setFoursides:(UIView *)view
          Direction:(NSString *)dirction
              sizeW:(CGFloat)sizew{
    
    if ([dirction  isEqual: @"left"]) {
        
        UIView *bottomview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0.5, sizew)];
        bottomview.backgroundColor = [UIColor colorWithRed:221.0/255.0 green:221.0/255.0 blue:221.0/255.0 alpha:1.0];
        [view addSubview:bottomview];
        
    }else if([dirction  isEqual: @"right"]){
        
        UIView *bottomview = [[UIView alloc] initWithFrame:CGRectMake(view.frame.size.width - 0.5, 0, 0.5, sizew)];
        bottomview.backgroundColor = [UIColor colorWithRed:221.0/255.0 green:221.0/255.0 blue:221.0/255.0 alpha:1.0];
        [view addSubview:bottomview];
        
    }else if([dirction  isEqual: @"top"]){
        
        UIView *bottomview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sizew, 0.5)];
        bottomview.backgroundColor = [UIColor colorWithRed:221.0/255.0 green:221.0/255.0 blue:221.0/255.0 alpha:1.0];
        [view addSubview:bottomview];
        
    }else if([dirction  isEqual: @"bottom"]){
        
        UIView *bottomview = [[UIView alloc] initWithFrame:CGRectMake(0, view.frame.size.height - 0.5, sizew, 0.5)];
        bottomview.backgroundColor = [UIColor colorWithRed:221.0/255.0 green:221.0/255.0 blue:221.0/255.0 alpha:1.0];
        [view addSubview:bottomview];
    }
}

+ (void)setUILabel:(UILabel *)label
              Data:(NSString *)string
           SetData:(NSString *)setstring
             Color:(UIColor *)color
              Font:(CGFloat)font
         Underline:(BOOL)isbool{
    
    NSRange str = [label.text rangeOfString:setstring];
    if (str.location != NSNotFound) {
        
        NSMutableAttributedString *str1 = [[NSMutableAttributedString alloc] initWithString:label.text];
        [str1 addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(string.length,setstring.length)]; //设置字体颜色
        [str1 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:font] range:NSMakeRange(string.length,setstring.length)];
        if (isbool) {
            
            [str1 addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(string.length,setstring.length)];
        }
        label.attributedText = str1;
    }
}


//蓝色长条按钮 需要重新设置origin
+ (UIButton *)createRNOLStyleBlueButtonWithButtonTitle:(NSString *)title {
    UIButton *button = [self createRNOLStyleButtonWithButtonTitle: title backgroundColor: kAppMainColor cornerRadius:0];
    return button;
}

//自定义颜色长条按钮 需要重新设置origin
+ (UIButton *)createRNOLStyleButtonWithButtonTitle:(NSString *)title backgroundColor:(UIColor *)bgColor cornerRadius:(
CGFloat)radius{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle: title  forState:UIControlStateNormal];
    [button setTitle: title forState: UIControlStateHighlighted];
    [button setBackgroundColor: bgColor forState: UIControlStateNormal];
    button.titleLabel.font = KFontSize(17);
    button.frame = CGRectMake(0, 0, KWIDTH, 50);
    [button setAllCorner: radius];
    return button;
}




#pragma mark - 富文本操作

/**
 *  单纯改变一句话中的某些字的颜色（一种颜色）
 *
 *  @param color    需要改变成的颜色
 *  @param totalStr 总的字符串
 *  @param subArray 需要改变颜色的文字数组(要是有相同的 只取第一个)
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)wgb_changeCorlorWithColor:(UIColor *)color TotalString:(NSString *)totalStr SubStringArray:(NSArray *)subArray{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
    for (NSString *rangeStr in subArray) {
        NSMutableArray *array = [self wgb_getRangeWithTotalString:totalStr SubString:rangeStr];
        for (NSNumber *rangeNum in array) {
            NSRange range = [rangeNum rangeValue];
            [attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
        }
    }
    return attributedStr;
}

#pragma mark - 获取某个子字符串在某个总字符串中位置数组
/**
 *  获取某个字符串中子字符串的位置数组
 *
 *  @param totalString 总的字符串
 *  @param subString   子字符串
 *
 *  @return 位置数组
 */
+ (NSMutableArray *)wgb_getRangeWithTotalString:(NSString *)totalString SubString:(NSString *)subString {
    
    NSMutableArray *arrayRanges = [NSMutableArray array];
    
    if (subString == nil && [subString isEqualToString:@""]) {
        return nil;
    }
    
    NSRange rang = [totalString rangeOfString:subString];
    
    if (rang.location != NSNotFound && rang.length != 0) {
        
        [arrayRanges addObject:[NSNumber valueWithRange:rang]];
        
        NSRange      rang1 = {0,0};
        NSInteger location = 0;
        NSInteger   length = 0;
        
        for (int i = 0;; i++) {
            
            if (0 == i) {
                
                location = rang.location + rang.length;
                length = totalString.length - rang.location - rang.length;
                rang1 = NSMakeRange(location, length);
            } else {
                
                location = rang1.location + rang1.length;
                length = totalString.length - rang1.location - rang1.length;
                rang1 = NSMakeRange(location, length);
            }
            
            rang1 = [totalString rangeOfString:subString options:NSCaseInsensitiveSearch range:rang1];
            
            if (rang1.location == NSNotFound && rang1.length == 0) {
                
                break;
            } else {
                
                [arrayRanges addObject:[NSNumber valueWithRange:rang1]];
            }
        }
        
        return arrayRanges;
    }
    
    return nil;
}


/**
 *  单纯改变句子的字间距（需要 <CoreText/CoreText.h>）
 *
 *  @param totalString 需要更改的字符串
 *  @param space       字间距
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)wgb_changeSpaceWithTotalString:(NSString *)totalString Space:(CGFloat)space{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    long number = space;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    [attributedStr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedStr length])];
    CFRelease(num);
    
    return attributedStr;
}

/**
 *  单纯改变段落的行间距
 *
 *  @param totalString 需要更改的字符串
 *  @param lineSpace   行间距
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)wgb_changeLineSpaceWithTotalString:(NSString *)totalString LineSpace:(CGFloat)lineSpace{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalString length])];
    return attributedStr;
}

/**
 *  同时更改行间距和字间距
 *
 *  @param totalString 需要改变的字符串
 *  @param lineSpace   行间距
 *  @param textSpace   字间距
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)wgb_changeLineAndTextSpaceWithTotalString:(NSString *)totalString LineSpace:(CGFloat)lineSpace textSpace:(CGFloat)textSpace{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalString length])];
    
    long number = textSpace;
    CFNumberRef num = CFNumberCreate(kCFAllocatorDefault,kCFNumberSInt8Type,&number);
    [attributedStr addAttribute:(id)kCTKernAttributeName value:(__bridge id)num range:NSMakeRange(0,[attributedStr length])];
    CFRelease(num);
    
    return attributedStr;
}
/**
 *  改变某些文字的颜色 并单独设置其字体
 *
 *  @param font        设置的字体
 *  @param color       颜色
 *  @param totalString 总的字符串
 *  @param subArray    想要变色的字符数组
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)wgb_changeFontAndColor:(UIFont *)font Color:(UIColor *)color TotalString:(NSString *)totalString SubStringArray:(NSArray *)subArray{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    for (NSString *rangeStr in subArray) {
        NSRange range = [totalString rangeOfString:rangeStr options:NSBackwardsSearch];
        [attributedStr addAttribute:NSForegroundColorAttributeName value:color range:range];
        [attributedStr addAttribute:NSFontAttributeName value:font range:range];
    }
    return attributedStr;
}

/**
 *  为某些文字改为链接形式
 *
 *  @param totalString 总的字符串
 *  @param subArray    需要改变颜色的文字数组(要是有相同的 只取第一个)
 *
 *  @return 生成的富文本
 */
+ (NSMutableAttributedString *)wgb_addLinkWithTotalString:(NSString *)totalString SubStringArray:(NSArray *)subArray{
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:totalString];
    for (NSString *rangeStr in subArray) {
        NSRange range = [totalString rangeOfString:rangeStr options:NSBackwardsSearch];
        [attributedStr addAttribute:NSLinkAttributeName value:totalString range:range];
    }
    return attributedStr;
}


+ (void)wgb_swizzleInstanceMethodWithClass:(Class)originClass
                          OriginMethod:(SEL)originMethod
                         swappedMethod:(SEL)swappedMethod {
    
    SEL originalSelector = originMethod;
    SEL swappedSelector = swappedMethod;
    
    Method originalMethod = class_getInstanceMethod(originClass, originalSelector);
    Method newMethod = class_getInstanceMethod(originClass, swappedSelector);
    
    BOOL didAddMethod = class_addMethod(originClass, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
    if (didAddMethod) {
        class_replaceMethod(originClass, swappedSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, newMethod);
    }
}

+ (void)wgb_swizzleClassMethodWithClass:(Class)originClass
                       OriginMethod:(SEL)originMethod
                      swappedMethod:(SEL)swappedMethod {
    SEL originalSelector = originMethod;
    SEL swappedSelector = swappedMethod;
    
    Method originalMethod = class_getClassMethod(originClass, originalSelector);
    Method newMethod = class_getClassMethod(originClass, swappedSelector);
    
    BOOL didAddMethod = class_addMethod(originClass, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
    if (didAddMethod) {
        class_replaceMethod(originClass, swappedSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, newMethod);
    }
}


@end
